package com.example.demo.security.sysGroup.controller;


import com.example.demo.security.sysGroup.entity.GroupBean;
import com.example.demo.security.sysGroup.service.GroupService;
import com.example.demo.security.sysUser.entity.UserBean;
import com.example.demo.security.sysUser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("group")
public class GroupController {


    @Autowired
    private GroupService groupService;
    @Autowired
    private UserService userService;

    private static final String file_path="security/sysGroup/";

    @RequestMapping("toAdd")
    public String toAdd(HttpServletRequest request){
        Integer id=Integer.parseInt(request.getSession().getAttribute("id").toString());
        List<UserBean> list=userService.queryFriends(id);
        request.setAttribute("list",list);
        return file_path+"addGroup";
    }
    @RequestMapping("toAddFriend")
    public String toAddFriend(HttpServletRequest request,Integer groupId){
        Integer id=Integer.parseInt(request.getSession().getAttribute("id").toString());
        List<UserBean> list=userService.queryNotInGroup(id,groupId);
        request.setAttribute("list1",list);
        request.setAttribute("groupId",groupId);
        return file_path+"addFriendGroup";
    }

    @RequestMapping("toExitGroup")
    @ResponseBody
    public String toExitGroup(HttpServletRequest request,Integer groupId){
        Integer id=Integer.parseInt(request.getSession().getAttribute("id").toString());
        try {
            Integer i=groupService.exitGroup(id,groupId);
            if (i==0){
                return "退群失败！";
            }else {
                return "退群成功！";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "系统异常！";
        }
    }

    /**
     * 加载用户群聊
     * @author 自嘲
     * @param request
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public List<GroupBean> list(HttpServletRequest request){
        List<GroupBean> list=null;
        try {
            String id=request.getSession().getAttribute("id").toString();
            list=groupService.queryGroups(Integer.parseInt(id));
        }catch (Exception e){
            list=null;
        }
        return list;
    }

    @RequestMapping("add")
    @ResponseBody
    public String add(HttpServletRequest request,
                      @RequestParam(value = "userIds[]",required=false) Integer[] userIds,String groupName){
        Integer id=Integer.parseInt(request.getSession().getAttribute("id").toString());
        try {
            Integer i=0;
            Integer groupId=groupService.addGroup(groupName,id);
            i+=groupService.addOneGroupMessage(groupId,id);
            if (userIds!=null){
                i+=groupService.addGroupMessage(groupId,userIds);
            }
            if (i==0&&userIds!=null){
                return "创建成功但邀请好友失败！";
            }else{
                return "创建成功！";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "系统异常！";
        }
    }
    @RequestMapping("addGroupFriend")
    @ResponseBody
    public String addGroupFriend(HttpServletRequest request,
                      @RequestParam(value = "userIds[]",required=false) Integer[] userIds,Integer groupId){
        try {
            Integer i=0;
            if (userIds!=null){
                i+=groupService.addGroupMessage(groupId,userIds);
            }
            if (i==0){
                return "邀请好友失败！";
            }else{
                return "邀请好友成功！";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "系统异常！";
        }
    }
}
